﻿@{
    Layout = "~/Views/Shared/_Dialog.cshtml";
}
<section class="container-fluid" style="background-color: white;">
    <div class="data-wrap case">
        <div class="data-tool clearfix">
            <div class="pull-left form-inline ">
            </div>
            <div class="pull-right">
                <form class="form-inline" id="formSearch">
                    <button type="button" class="btn btn-default btn-sm active f_l" id="btnSearch" onclick="return insertRow();"><span class="glyphicon glyphicon-plus"></span> 添加行</button>
                </form>
            </div>
        </div>
        <div class="data-div-view table-responsive">
            <table class="table table-hover table-striped table-bordered paymentRecord">
                <thead>
                    <tr>
                        <th class="t_l">商户订单号</th>
                        <th class="t_l wd120">姓名</th>
                        <th class="t_l wd120">卡号</th>
                        <th class="t_l wd75">金额</th>
                        <th class="t_l wd75">银行名字</th>
                        <th class="t_c wd75">是否推送</th>
                        <th class="t_c wd30">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="t_l wd100"> <input class="form-control" type="text" name="orderNo" maxlength="120" value="" autocomplete="off" placeholder=""></td>
                        <td class="t_l wd120"> <input class="form-control" type="text" name="name" maxlength="120" value="" autocomplete="off" placeholder=""></td>
                        <td class="t_l wd150"> <input class="form-control" type="text" oldvalue="" oninput="cardNumberChange(this)" name="cardNumber" maxlength="120" value="" autocomplete="off" placeholder=""></td>
                        <td class="t_l wd100"> <input class="form-control" type="text" name="amount" maxlength="120" value="" autocomplete="off" placeholder=""></td>
                        <td class="t_l wd150"> <input class="form-control" type="text" name="bankName" maxlength="120" value="" autocomplete="off" placeholder=""></td>
                        <td class="t_c wd75"> <input type="checkbox" name="push"></td>
                        <td class="t_c wd30">
                            <a class="btn btn-danger btn-xs" data-confirm="确定要禁用该商户？" data-succesfun="doSearch()" onclick="return removeRow(this)">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</section>
@section footerScript{
    <script type="text/x-dot-template" id="tpl">
        <tr>
            <td class="t_l wd100"> <input class="form-control" type="text" name="orderNo" maxlength="120" value="" autocomplete="off" placeholder=""></td>
            <td class="t_l wd120"> <input class="form-control" type="text" name="name" maxlength="120" value="" autocomplete="off" placeholder=""></td>
            <td class="t_l wd150"> <input class="form-control" type="text" oldvalue="" oninput="cardNumberChange(this)" name="cardNumber" maxlength="120" value="" autocomplete="off" placeholder=""></td>
            <td class="t_l wd100"> <input class="form-control" type="text" name="amount" maxlength="120" value="" autocomplete="off" placeholder=""></td>
            <td class="t_l wd150"> <input class="form-control" type="text" name="bankName" maxlength="120" value="" autocomplete="off" placeholder=""></td>
            <td class="t_c wd75"> <input type="checkbox" name="push"></td>
            <td class="t_c wd30">
                <a class="btn btn-danger btn-xs" data-confirm="确定要禁用该商户？" data-succesfun="doSearch()" onclick="return removeRow(this)">删除</a>
            </td>
        </tr>
    </script>
    <script type="text/javascript">

        function cardNumberChange(obj) {
            var value = $(obj).val();
            if ($(obj).attr("oldvalue") != value) {
                FlashPay.Util.Ajax({
                    type: "GET",
                    url: `/bank/GetBankNameByCard?cardNo=${value}`,
                    success: function (res) {
                        if (res) {
                            $(obj).parents("tr").find("input[name='bankName']").val(res.bankName);
                        }
                    },
                    error: function (json) {
                        cover.Remove();
                        FlashPay.UI.Tip_warning("操作失败！请查看网络，请重试。");
                    }
                });
            }
            $(obj).attr("oldvalue", value);
        }

        function insertRow() {
            var length = $("table.paymentRecord > tbody > tr").length;

            if (length >= 5) {
                FlashPay.UI.Tip_warning("最大5条记录。");
                return false;
            }
            var html = doT.template($("#tpl").text());
            $("table.paymentRecord tbody").append(html);
        }
        function removeRow(obj) {
            $(obj).parent().parent().remove();
        }

        function isNum(value) {
            var isNum = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
            var result = false;
            if (isNum.test(value)) {
                result = true;
            }
            return result;
        }

        function save() {

            var cover = FlashPay.UI.Mask({
                obj: $("body"),
                opacity: 0.5
            });

            var paymentRecords = new Array();

            $("table.paymentRecord>tbody>tr").each(function (d) {

                var orderNo = $(this).find("input[name='orderNo']").val();
                var name = $(this).find("input[name='name']").val();
                var cardNumber = $(this).find("input[name='cardNumber']").val();
                var amount = $(this).find("input[name='amount']").val();
                var push = $(this).find("input[name='push']").is(":checked");
                var bankName = $(this).find("input[name='bankName']").val();

                if (push && $.trim(orderNo) == '') {
                    FlashPay.UI.Tip_short_warning("选择推送时，商户订单号必须填写！");

                    cover.Remove();
                    return false;
                }

                if ($.trim(name) == '' || $.trim(cardNumber) == '' || $.trim(amount) == '' || $.trim(bankName) == '') {

                    FlashPay.UI.Tip_short_warning("姓名，卡号，金额，银行名字都必须填写！");

                    cover.Remove();
                    return false;
                }

                if (!isNum(amount)) {
                    FlashPay.UI.Tip_short_warning("请确认金额是否输入正确！");
                    cover.Remove();
                    return false;
                }

                var model = {
                    MerchantOrderNo: orderNo,
                    WithdrawalCardName: name,
                    WithdrawalCardNumber: cardNumber,
                    WithdrawalAmount: amount,
                    Push: push,
                    WithdrawalBankName:bankName
                };

                if (FlashPay.Util.isNullOrEmptySpance(push)) {
                    model.Push = 0;
                } else {
                    model.Push = 1;
                }
                paymentRecords.push(model);
            });

            console.log(paymentRecords);

            FlashPay.Util.Ajax({
                type: "POST",
                url: "/Paymentrecord/BatchAdd",
                data: JSON.stringify(paymentRecords),
                contentType: 'application/json;charset=utf-8', 
                success: function (res) {
                    if (res.success) {
                        cover.Remove();
                        FlashPay.UI.DialogOpener().Tip_success("操作成功！");
                        FlashPay.UI.DialogOpener().doSearch();
                        FlashPay.UI.CloseDialog();
                    } else {
                        cover.Remove();
                        FlashPay.UI.Tip_short_warning("操作失败！" + res.message);
                    }
                },
                error: function (json) {
                    cover.Remove();
                    FlashPay.UI.Tip_warning("操作失败！请查看网络，请重试。");
                }
            });

        }
    </script>
}